package com.wntms.gps.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wntms.gps.service.IDriverInformationService;
import com.wntms.gps.pojo.CarriageContract;
import com.wntms.gps.pojo.DriverInformation;
import com.wntms.gps.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 司机信息Controller
 *
 * @author group 4
 * @date 2022-11-22
 */
@RestController
@RequestMapping("/driver")
public class DriverInformationController
{
    @Autowired
    private IDriverInformationService driverInformationService;

    /**
     * 查询司机信息列表
     */

    @GetMapping("/list")
    public Result list(DriverInformation driverInformation,Integer currentPage,Integer pageSize)
    {
        PageHelper.startPage(currentPage,pageSize);

        List<DriverInformation> list = driverInformationService.selectDriverInformationList(driverInformation);

        PageInfo<DriverInformation> pageInfo = new PageInfo<>(list);

//        PageInfo<CarriageContract> pageInfo = PageHelper.startPage(currentPage,pageSize).doSelectPageInfo(
//                ()->driverInformationService.selectDriverInformationList(driverInformation));

        Result result = new Result();
        result.setCode(200);
        result.setMsg("success");
        result.setData(pageInfo);

        return result;
    }

    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        return Result.success(driverInformationService.selectDriverInformationById(id),"查询成功");
    }

    /**
     * 新增司机信息
     */

    @PostMapping
    public Result add(@RequestBody DriverInformation driverInformation)
    {
        Result result = new Result();
        int i = driverInformationService.insertDriverInformation(driverInformation);
        if(i!=0){
            result.setCode(200);
            result.setMsg("插入成功");
        }else{
            result.setCode(500);
            result.setMsg("插入失败");
        }

        return result;
    }

    /**
     * 修改司机信息
     */

    @PutMapping
    public Result edit(@RequestBody DriverInformation driverInformation)
    {
        Result result = new Result();
        int i = driverInformationService.updateDriverInformation(driverInformation);
        if(i!=0){
            result.setCode(200);
            result.setMsg("更新成功");
        }else{
            result.setCode(500);
            result.setMsg("更新失败");
        }

        return result;
    }

    /**
     * 删除司机信息
     */

	@DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        Result result = new Result();
        int i = driverInformationService.deleteDriverInformationByIds(ids);
        if(i!=0){
            result.setCode(200);
            result.setMsg("共删除"+i+"条记录");
        }else{
            result.setCode(500);
            result.setMsg("删除失败");
        }

        return result;
    }
}
